package com.admin.socketmultiplayer.config

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import android.widget.ImageView
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.b.t
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.admin.socketmultiplayer.R
import com.admin.socketmultiplayer.bean.MyUser
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.socks.library.KLog
import java.io.File
import java.util.*
import java.util.regex.Pattern
import java.util.Arrays.asList



/**
 * Created by 吴昶 on 2018/4/27.
 */
class DataUtils{

    companion object {
        fun isPhoneNumRight(data: String,key:String): Boolean {
            val p = Pattern.compile(key)
            val m = p.matcher(data)
            val isMatch = m.matches()
            return isMatch
        }

        fun <T> paresJsonWithGon(string: String,t :Class<T>):T{
            var gson= Gson()
            var result=gson.fromJson<T>(string,t)
            return result
        }


        fun <T> jsonToList(json: String, clazz: Class<out Array<T>>): Array<T> {
            val gson = Gson()
            val array = gson.fromJson<Array<T>>(json, clazz)
            return array

        }

        fun getUriFormFile(context: Context,path:String):Uri{
            var uri:Uri?=null
            var u=File(path)
            uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                // android 7.0+以上版本 使用FileProvider打开文件
                FileProvider.getUriForFile(context, "com.admin.socketmultiplayer.fileprovider", u)
            } else {
                Uri.fromFile(u)
            }
            return uri
        }

        fun showUserImage(context: Context,userId:String,image:ImageView){
            var query=BmobQuery<MyUser>()
            query.addWhereEqualTo("objectId",userId)
            query.findObjects(object :FindListener<MyUser>(){
                override fun done(p0: MutableList<MyUser>?, p1: BmobException?) {
                    if(p0!=null&&p0.size>0){
                        KLog.d(p0[0].toString())
                        var h=p0[0].header
                        h?.let {
                            Glide.with(context).load(h.fileUrl).into(image)
                        }
                        h?:let {
                            image.setImageResource(R.mipmap.ioc_user_hander2)
                        }
                    }else{
                        image.setImageResource(R.mipmap.ioc_user_hander2)
                    }
                }
            })
        }

    }
}